<?php

/**
 * undocumented class
 *
 * @package default
 * @author Marc
 **/
class uvmcSolrSearchForm extends sfForm
{
  protected 
    $facets = array();
  
  /**
   * setup the defaults for a uvmcSolrSearchForm
   * - disable csrf protection
   * - allow extra fields (warning, you might want to turn it sometimes)
   * - create the query field
   *
   */
  public function setup()
  {
    $this->disableCsrfProtection();
    $this->validatorSchema->setOption('allow_extra_fields', true);
    
    $this->widgetSchema['q'] = new sfWidgetFormInput();
    $this->validatorSchema['q'] = new sfValidatorString(array('required' => true,
                                                              'max_length' => true));
  
    $this->widgetSchema->setLabel('q', "What are you searching for?");
    $this->widgetSchema->setNameFormat('solr[%s]');
  }
  
  /**
   * Generate widgets for facets
   * @param $facets array
   */
  public function generateWidgetsFromFacets($facets = array())
  {
    if (empty($facets) && empty($this->facets))
    {
      return;
    }
    else if (!empty($facets))
    {
      $this->setFacets($facets);
    }
    
    foreach ($this->facets['facet_fields'] as $name => $values)
    {
      $choices = $this->formatFacetValuesAsChoices($values);
      if(!empty($choices))
      {
        $this->widgetSchema[$name] = new sfWidgetFormChoiceMany(array('choices' => $choices,
                                                                      'renderer_class' => 'sfWidgetFormSelectCheckbox', ));
        $this->validatorSchema[$name] = new sfValidatorChoiceMany(array('choices' => array_keys($choices),
                                                                    'required' => false));
      }
    }
  }
  
  /**
   * Set the facets from which the widgets will be generated
   */
  public function setFacets($facets)
  {
    $this->facets = $facets;
  }
  
  /**
   * hide query widget
   */
  public function hideQueryWidget()
  {
    $this->widgetSchema['q']->setOption('is_hidden', true);
    $this->widgetSchema['q']->setOption('type', 'hidden');
  }
  
  /**
   * Create a valid solr query from widgets value
   */
  public function formatAsSolrQuery()
  {
    if (!$this->isBound())
    {
      throw new sfException('uvmcSolrSearchForm needs to be bound before transformed as a solr query');
    }
    
    $q = uvmcSolrQuery::create()->andTerm($this->getValue('q'));
    foreach($this->getValues() as $field => $value)
    { 
      if($field != 'q')
      {
        if(!empty($value))
        {
          $q->andField($field, $value);
        }
      }
    }
    
    return $q->getQueryString();
  }
  
  /**
   * format facet values as choices which can be used for formWidgets
   * @param $facet array
   * @return array
   */ 
  protected function formatFacetValuesAsChoices($facet)
  {
    $choices = array();
    foreach($facet as $k => $v)
    {
      if(!empty($k) && !empty($v))
      {
        $choices[$k] = $k." (".$v.")";
      }
    }
    
    return $choices;
  }
}